package zonedao

import (
	"github.com/gogf/gf/v2/frame/g"
	"xr-game-server/core/constants/common"
	"xr-game-server/core/constants/db"
	"xr-game-server/dto/serverdto"
	"xr-game-server/entity"
)

var zones = make([]*entity.Zone, common.Zero)
var zoneMap = make(map[uint64]*entity.Zone)

func Save(server *entity.Zone) {
	g.Model(string(entity.TbZone)).Save(server)
}

func InitZones() {
	g.Model(string(entity.TbZone)).Unscoped().OrderAsc(string(db.IdName)).Scan(&zones)
	for _, zone := range zones {
		zoneMap[zone.ID] = zone
	}
}

func GetZone(id uint64) *entity.Zone {
	val, ok := zoneMap[id]
	if ok {
		return val
	}
	return nil
}

func Page(req *serverdto.GetZoneReq) (total int, data []*entity.Zone) {
	data = make([]*entity.Zone, common.Zero)
	g.Model(string(entity.TbZone)).Unscoped().OrderAsc(string(db.IdName)).Limit(req.PageIndex, req.PageSize).ScanAndCount(&data, &total, false)
	return total, data
}
